//
//  ShopTimeViewController.m
//  SkinerZTZ
//
//  Created by cts on 2018/1/8.
//  Copyright © 2018年 hhjiang. All rights reserved.
//

#import "ShopTimeViewController.h"
#import "Header.h"
#import "ShopTimeCell.h"
#import "NSDate+GFCalendar.h"
#import "TimeModel.h"

@interface ShopTimeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    
    NSString *seleTime;
    NSDate *todayDate;
    NSArray *timeArr ;
    NSArray *weekArr ;
    //当前时间
    NSInteger year ;
    NSInteger month ;
    NSInteger day ;
    NSInteger hour ;
    NSInteger min ;
    
    NSInteger seleIndex;
    
    NSInteger *index;
    
    NSInteger seleIndex1;
    
    NSArray *arrtime1;
    NSArray *arrtime2;
    NSArray *arrtime3;
    NSArray *arrtime4;
    NSArray *arrtime5;
    NSArray *arrtime6;
}
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *timeArray;
@property(nonatomic,strong) UICollectionView *collectView;
@property(nonatomic,strong) UIView *myline;
@end

@implementation ShopTimeViewController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)timeArray
{
    if (!_timeArray) {
        _timeArray = [NSMutableArray array];
    }
    return _timeArray;
}
-(UICollectionView *)collectView
{
    if (!_collectView) {
        //1.初始化layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置collectionView滚动方向
        //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        //设置headerView的尺寸大小
      //  layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
        //该方法也可以设置itemSize
        CGFloat kW = (kScreenWidth - 60) / 4;
        layout.itemSize =CGSizeMake(kW, 40);
        
        //2.初始化collectionView
      UICollectionView  *mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 124, kScreenWidth, kScreenHeight - 124) collectionViewLayout:layout];
        [self.view addSubview:mainCollectionView];
        mainCollectionView.backgroundColor = [UIColor clearColor];
        
        //3.注册collectionViewCell
        //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
        [mainCollectionView registerClass:[ShopTimeCell class] forCellWithReuseIdentifier:@"ShopTimeCell"];
        
        //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
      //  [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
        
        //4.设置代理
        mainCollectionView.delegate = self;
        mainCollectionView.dataSource = self;
        _collectView = mainCollectionView;
    
    }
    return _collectView;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    seleIndex = 0;
    
    todayDate = [NSDate date];
    NSString *str = [todayDate getWeekTime];
    [self getCurrenTime:todayDate];
    NSArray *arr = [str componentsSeparatedByString:@"="];
    timeArr = [arr[0] componentsSeparatedByString:@"+"];
    weekArr = [arr[1] componentsSeparatedByString:@"+"];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的排期";
    MyNavView *barview = [[MyNavView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64) Title:@"我的排期"];
    [barview.leftBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [barview.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [barview.rightBtn setTitle:@"全选" forState:UIControlStateNormal];
    [barview.rightBtn addTarget:self action:@selector(quedingClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:barview];
    NSArray *timearr  = @[@"08:00",@"08:30",@"09:00",@"09:30",@"10:00",@"10:30",@"11:00",@"11:30",@"12:00",@"12:30",@"13:00",@"13:30",@"14:00",@"14:30",@"15:00",@"15:30",@"16:00",@"16:30",@"17:00",@"17:30",@"18:00",@"18:30",@"19:00",@"19:30",@"20:00",@"20:30",@"21:00",@"21:30"];
    for (NSInteger i = 0; i < timearr.count; i++) {
        TimeModel *model = [TimeModel new];
        model.timeStr = timearr[i];
        model.isOutTime = NO;
        NSString *hourstr = [model.timeStr substringToIndex:2];
        NSString *minStr = [model.timeStr substringFromIndex:3];
        if ([hourstr integerValue] < hour) {
            model.isOutTime = YES;
        }else{
            if ([hourstr integerValue] == hour) {
                if ([minStr integerValue] < min) {
                    model.isOutTime = YES;
                }
            }
        }
        
        model.isSeleTime = NO;
        [self.dataArray addObject:model];
        
    }
    
    [self createUI];
    [self collectView];
    if ([self.type isEqualToString:@"2"]) {
        [self dataForNetwork];
    }
    
    
}
-(void)createUI{
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 60)];
   
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    
    CGFloat kW = kScreenWidth / 6;
    for (NSInteger i = 0; i < 6; i++) {
        UIButton *lable = [[UIButton alloc] initWithFrame:CGRectMake(i *kW, 0, kW, 50)];
        NSString *week = [self getWeekDay:[weekArr[i] integerValue]];
        NSString *str = [NSString stringWithFormat:@"%@\n%@",week ,timeArr[i]];
        [lable setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [lable setTitleColor:KCommonColor forState:UIControlStateSelected];
        [lable setTitle:str forState:UIControlStateNormal];
        lable.titleLabel.font = kFONT_HEITI_TC(13);
        lable.tag = i +1;
        lable.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        lable.titleLabel.textAlignment = NSTextAlignmentCenter;
        [topView addSubview:lable];
        if (i == 0) {
            lable.selected = YES;
        }
        [lable addTarget:self action:@selector(lableClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.myline = [[UIView alloc] initWithFrame:CGRectMake(0, 50, kW, 2)];
    self.myline.backgroundColor = KCommonColor;
    [topView addSubview:self.myline];
    
}

#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TimeModel *model = self.dataArray[indexPath.item];
    ShopTimeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShopTimeCell" forIndexPath:indexPath];
    cell.timel.text = model.timeStr;
    cell.timel.textColor = [UIColor lightGrayColor];
    cell.timel.layer.borderWidth = 1;
    cell.timel.layer.borderColor = color(235, 235, 235).CGColor;
    cell.timel.backgroundColor = [UIColor whiteColor];
    cell.timel.layer.cornerRadius = 3;
    cell.timel.layer.masksToBounds = YES;
    if (index == indexPath.item && seleIndex == seleIndex1) {
        cell.timel.textColor = KCommonColor;
        cell.timel.layer.borderColor = KCommonColor.CGColor;
        cell.timel.backgroundColor = color(255, 239, 242);
    }
    //是否时间已过
    if (model.isOutTime && seleIndex == 0) {
        cell.timel.backgroundColor = color(235, 235, 235);
        cell.timel.textColor = [UIColor lightGrayColor];
        cell.timel.layer.borderWidth = 1;
        cell.timel.layer.borderColor = color(235, 235, 235).CGColor;
    }
    if (seleIndex == 0) {
        for (NSInteger i = 0; i < arrtime1.count; i++) {
            if ([arrtime1[i] integerValue] - 1 == indexPath.item) {
                cell.timel.textColor = KCommonColor;
                cell.timel.layer.borderColor = KCommonColor.CGColor;
                cell.timel.backgroundColor = color(255, 239, 242);
                
                
            }else{
                if ([arrtime1[i] integerValue] - 2 == indexPath.item || [arrtime1[i] integerValue]  == indexPath.item || [arrtime1[i] integerValue] + 1 == indexPath.item) {
                    cell.timel.backgroundColor = color(235, 235, 235);
                    cell.timel.textColor = [UIColor lightGrayColor];
                    cell.timel.layer.borderWidth = 1;
                    cell.timel.layer.borderColor = color(235, 235, 235).CGColor;
                }
            }
        }
    }else if (seleIndex == 1){
        for (NSInteger i = 0; i < arrtime2.count; i++) {
            if ([arrtime2[i] integerValue] - 1 == indexPath.item) {
                cell.timel.textColor = KCommonColor;
                cell.timel.layer.borderColor = KCommonColor.CGColor;
                cell.timel.backgroundColor = color(255, 239, 242);
            }else{
                if ([arrtime2[i] integerValue] - 2 == indexPath.item || [arrtime2[i] integerValue]  == indexPath.item || [arrtime2[i] integerValue] + 1 == indexPath.item) {
                    cell.timel.backgroundColor = color(235, 235, 235);
                    cell.timel.textColor = [UIColor lightGrayColor];
                    cell.timel.layer.borderWidth = 1;
                    cell.timel.layer.borderColor = color(235, 235, 235).CGColor;
                }
            }
        }
    }else if (seleIndex == 2){
        for (NSInteger i = 0; i < arrtime3.count; i++) {
            if ([arrtime3[i] integerValue] - 1 == indexPath.item) {
                cell.timel.textColor = KCommonColor;
                cell.timel.layer.borderColor = KCommonColor.CGColor;
                cell.timel.backgroundColor = color(255, 239, 242);
            }else{
                if ([arrtime3[i] integerValue] - 2 == indexPath.item || [arrtime3[i] integerValue]  == indexPath.item || [arrtime3[i] integerValue] + 1 == indexPath.item) {
                    cell.timel.backgroundColor = color(235, 235, 235);
                    cell.timel.textColor = [UIColor lightGrayColor];
                    cell.timel.layer.borderWidth = 1;
                    cell.timel.layer.borderColor = color(235, 235, 235).CGColor;
                }
            }
        }
    }else if (seleIndex == 3){
        for (NSInteger i = 0; i < arrtime4.count; i++) {
            if ([arrtime4[i] integerValue] - 1 == indexPath.item) {
                cell.timel.textColor = KCommonColor;
                cell.timel.layer.borderColor = KCommonColor.CGColor;
                cell.timel.backgroundColor = color(255, 239, 242);
            }else{
                if ([arrtime4[i] integerValue] - 2 == indexPath.item || [arrtime4[i] integerValue]  == indexPath.item || [arrtime4[i] integerValue] + 1 == indexPath.item) {
                    cell.timel.backgroundColor = color(235, 235, 235);
                    cell.timel.textColor = [UIColor lightGrayColor];
                    cell.timel.layer.borderWidth = 1;
                    cell.timel.layer.borderColor = color(235, 235, 235).CGColor;
                }
            }
        }
    }else if (seleIndex == 4){
        for (NSInteger i = 0; i < arrtime5.count; i++) {
            if ([arrtime5[i] integerValue] - 1 == indexPath.item) {
                cell.timel.textColor = KCommonColor;
                cell.timel.layer.borderColor = KCommonColor.CGColor;
                cell.timel.backgroundColor = color(255, 239, 242);
            }else{
                if ([arrtime5[i] integerValue] - 2 == indexPath.item || [arrtime5[i] integerValue]  == indexPath.item || [arrtime5[i] integerValue] + 1 == indexPath.item) {
                    cell.timel.backgroundColor = color(235, 235, 235);
                    cell.timel.textColor = [UIColor lightGrayColor];
                    cell.timel.layer.borderWidth = 1;
                    cell.timel.layer.borderColor = color(235, 235, 235).CGColor;
                }
            }
        }
    }else if (seleIndex == 5){
        for (NSInteger i = 0; i < arrtime6.count; i++) {
            if ([arrtime6[i] integerValue] - 1 == indexPath.item) {
                cell.timel.textColor = KCommonColor;
                cell.timel.layer.borderColor = KCommonColor.CGColor;
                cell.timel.backgroundColor = color(255, 239, 242);
            }else{
                if ([arrtime6[i] integerValue] - 2 == indexPath.item || [arrtime6[i] integerValue]  == indexPath.item || [arrtime6[i] integerValue] + 1 == indexPath.item) {
                    cell.timel.backgroundColor = color(235, 235, 235);
                    cell.timel.textColor = [UIColor lightGrayColor];
                    cell.timel.layer.borderWidth = 1;
                    cell.timel.layer.borderColor = color(235, 235, 235).CGColor;
                }
            }
        }
    }
    
  
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
     CGFloat kW = (kScreenWidth - 60) / 4;
    return CGSizeMake(kW, 34);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  
    TimeModel *model = self.dataArray[indexPath.item];
    if (model.isOutTime && seleIndex == 0) {
       
        return;
    }
    if (seleIndex == 0) {
        for (NSInteger i = 0; i < arrtime1.count; i++) {
            if ([arrtime1[i] integerValue] - 1 == indexPath.item || [arrtime1[i] integerValue] - 2 == indexPath.item || [arrtime1[i] integerValue]  == indexPath.item || [arrtime1[i] integerValue] + 1 == indexPath.item) {
               
               return;
            }
        }
    }else if (seleIndex == 1){
        for (NSInteger i = 0; i < arrtime2.count; i++) {
            if ([arrtime2[i] integerValue] - 1 == indexPath.item || [arrtime2[i] integerValue] - 2 == indexPath.item || [arrtime2[i] integerValue]  == indexPath.item || [arrtime2[i] integerValue] + 1 == indexPath.item) {
               return;
            }
        }
    }else if (seleIndex == 2){
        for (NSInteger i = 0; i < arrtime3.count; i++) {
            if ([arrtime3[i] integerValue] - 1 == indexPath.item || [arrtime3[i] integerValue] - 2 == indexPath.item || [arrtime3[i] integerValue]  == indexPath.item || [arrtime3[i] integerValue] + 1 == indexPath.item) {
               return;
            }
        }
    }else if (seleIndex == 3){
        for (NSInteger i = 0; i < arrtime4.count; i++) {
            if ([arrtime4[i] integerValue] - 1 == indexPath.item || [arrtime4[i] integerValue] - 2 == indexPath.item || [arrtime4[i] integerValue]  == indexPath.item || [arrtime4[i] integerValue] + 1 == indexPath.item) {
             return;
            }
        }
    }else if (seleIndex == 4){
        for (NSInteger i = 0; i < arrtime5.count; i++) {
            if ([arrtime5[i] integerValue] - 1 == indexPath.item || [arrtime5[i] integerValue] - 2 == indexPath.item || [arrtime5[i] integerValue]  == indexPath.item || [arrtime5[i] integerValue] + 1 == indexPath.item) {
             return;
            }
        }
    }else if (seleIndex == 5){
        for (NSInteger i = 0; i < arrtime6.count; i++) {
            if ([arrtime6[i] integerValue] - 1 == indexPath.item || [arrtime6[i] integerValue] - 2 == indexPath.item || [arrtime6[i] integerValue]  == indexPath.item || [arrtime6[i] integerValue] + 1 == indexPath.item) {
           return;
            }
        }
    }
    
    seleTime = model.timeStr;
    
    seleIndex1 = seleIndex;
    index = indexPath.item;
    [collectionView reloadData];
    
}

#pragma mark - click
//哪一天
-(void)lableClick:(UIButton *)btn{
    for (NSInteger i = 0; i <6; i++) {
         UIButton *btn1 = (UIButton *)[self.view viewWithTag:i+1];
        if ([btn1 isKindOfClass:[UIButton class]]) {
             btn1.selected = NO;
        }
       
    }
    btn.selected = YES;
    [UIView animateWithDuration:0.2 animations:^{
        CGRect frame = self.myline.frame;
        frame.origin.x = kScreenWidth/6 * (btn.tag -1);
        self.myline.frame = frame;
    }];
    seleIndex = btn.tag - 1;
    [self.collectView reloadData];
}

//获取星期
-(NSString *)getWeekDay:(NSInteger )week
{
    if (week == 1) {
        return [NSString stringWithFormat:@"星期天"] ;
    }else if (week == 2)
    {
        return [NSString stringWithFormat:@"星期一"] ;
    }
    else if (week == 3)
    {
        return [NSString stringWithFormat:@"星期二"] ;
    }else if (week == 4)
    {
        return [NSString stringWithFormat:@"星期三"] ;
    }else if (week == 5)
    {
        return [NSString stringWithFormat:@"星期四"] ;
    }else if (week == 6)
    {
        return [NSString stringWithFormat:@"星期五"] ;
    }else     {
        return [NSString stringWithFormat:@"星期六"] ;
    }
    
}
//获取当前时间
-(void)getCurrenTime:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:kCFCalendarUnitYear |kCFCalendarUnitMonth |NSCalendarUnitDay |NSCalendarUnitHour  | NSCalendarUnitMinute  fromDate:date];
    year = components.year;
    month = components.month;
    day = components.day;
    hour = components.hour;
    min = components.minute;
    
   
    
}

//返回
-(void)backClick{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)quedingClick{
    if ([seleTime isEqualToString:@""] || !seleTime) {
//        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//        hud.mode = MBProgressHUDModeText;
//        hud.label.text = @"请选择预约时间";
//        [hud hideAnimated:YES afterDelay:0.8f];
        return;
    }
    NSString *str = [NSString stringWithFormat:@"%ld-%@ %@",year,timeArr[seleIndex],seleTime];
    NSLog(@"str= %@",str);
    if (_kseleCallBack) {
        _kseleCallBack(str);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - request
-(void)dataForNetwork{
    
    AFNetworkingManager *manager = [AFNetworkingManager sharedInstance];
    NSString *timestr = [NSString stringWithFormat:@"%ld-%02ld-%02ld",year,month,day];
    [manager GET:@"" parameters:@{@"createTime_":timestr,@"id_":self.id_} Withsuccess:^(id responseObject) {
        NSLog(@"responseObject = %@",responseObject);
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
         
            NSArray *arr = responseObject[@"data"];
            for (NSDictionary *dict in arr) {
                NSString *str = dict[@"time"];
                [self.timeArray addObject:str];
            }
            if (![self.timeArray[0] isEqualToString:@""]) {
                arrtime1 = [self.timeArray[0] componentsSeparatedByString:@","];
            }
            if (![self.timeArray[1] isEqualToString:@""]) {
                arrtime2 = [self.timeArray[1] componentsSeparatedByString:@","];
            }
            if (![self.timeArray[2] isEqualToString:@""]) {
                arrtime3 = [self.timeArray[2] componentsSeparatedByString:@","];
            }
            if (![self.timeArray[3] isEqualToString:@""]) {
                arrtime4 = [self.timeArray[3] componentsSeparatedByString:@","];
            }
            if (![self.timeArray[4] isEqualToString:@""]) {
                arrtime5 = [self.timeArray[4] componentsSeparatedByString:@","];
            }
            if (![self.timeArray[5] isEqualToString:@""]) {
                arrtime6 = [self.timeArray[5] componentsSeparatedByString:@","];
            }
            
            
            
            
    
            [self.collectView reloadData];
        }
        
    } WithFailure:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
    
    
    
}

@end
